<?php
namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\OrderInfo;
use app\models\OrderInfoGood;
use app\tools\Re;
use app\tools\AliRefund;
use Yii;

class OrderController extends BaseController
{
    public $state_arr = ['待付款', '待发货', '待收货', '售后中', '已完成', '退款中', '退款成功', '退款失败', '已取消'];

    //订单列表
  	public function actionOrderList() {
  		$page = Yii::$app->request->post('page', 0);
      $phone = Yii::$app->request->post('phone', '');
      $order_sn = Yii::$app->request->post('order_sn', '');
      $state = Yii::$app->request->post('state', -2);
      $date_range = Yii::$app->request->post('created_at_range', '');

      $order_query = OrderInfo::find()
        ->leftJoin('user', 'order_info.user_id = user.id')
        ->where(['relate_id' => 0]);
      if ($phone) {
        $order_query->andWhere(['like', 'phone', $phone]);
      }
      if ($date_range) {
        $data_arr = explode("-", $date_range);
        $start_at = $data_arr[0];
        $end_at = $data_arr[1];
        $order_query->andWhere("order_info.created_at >= '$start_at' and order_info.created_at <= '$end_at'");
      }
      if ($order_sn) {
        $order_query->andWhere(['like', 'order_sn', $order_sn]);
      }
      if ($state > -1) {
        $order_query->andWhere(['state' => $state]);
      }
  		$list = $order_query->orderBy("created_at desc")->limit(OrderInfo::PAGE_SIZE)->offset(OrderInfo::PAGE_SIZE*$page)->all();
        $order_data = [];
        if ($list) {
          foreach ($list as $v) {
            $good_data = [];
            $good_list = OrderInfoGood::find()->where(['order_id' => $v->id])->all();
            if ($good_list) {
              foreach ($good_list as $v1) {
                $good_data[] = [
                  'id' => $v1->id,
                  'good_name' => $v1->good->name,
                  'good_img' => $v1->good->img,
                  'good_price' => $v1->price,
                  'good_num' => $v1->num
                ];
              }
            }
            $order_data[] = [
              'id' => $v->id,
              'name' => $v->user->name,
              'phone' => $v->user->phone,
              'order_sn' => $v->order_sn,
              'money' => $v->money,
              'state' => $v->state,
              'state_name' => $this->state_arr[$v->state],
              'created_time' => date("Y-m-d H:i:s", $v->created_at),
              'pay_time' => $v->pay_at ? date('Y-m-d H:i:s', $v->pay_at) : '',
              'address_name' => $v->address->name,
              'address_phone' => $v->address->phone,
              'address' => $v->address,
              'good_data' => $good_data,
            ];
          }
        }
        return Re::data([
          'data' => $order_data,
          'totalPage' => $order_query->count(),
          'currentPage' => $page,
          'perPage' => OrderInfo::PAGE_SIZE
        ]);
  	}

    //订单详情
    public function actionOrderInfo() {
      $id = Yii::$app->request->post('id', 0);
      $order = OrderInfo::findOne($id);
      if (empty($order)) {
        return Re::data(null, '订单信息读取失败', Re::ERROR);
      }
      $good_list = OrderInfoGood::find()->where(['order_id' => $id])->all();
      $good_data = [];
      if ($good_list) {
        foreach ($good_list as $v1) {
          $good_data[] = [
            'id' => $v1->id,
            'good_name' => $v1->good->name,
            'good_img' => $v1->good->img,
            'good_price' => $v1->price,
            'good_num' => $v1->num
          ];
        }
      }
      $order_data = [
        'id' => $order->id,
        'name' => $order->user->name,
        'phone' => $order->user->phone,
        'invitation_name' => $order->user->invitation_id ? $order->user->invitation->name : '/',
        'order_sn' => $order->order_sn,
        'money' => $order->money,
        'state' => $order->state,
        'state_name' => $this->state_arr[$order->state],
        'address_name' => $order->address->name,
        'address_phone' => $order->address->phone,
        'address' => $order->address,
        'order_send_code' => $order->order_send_code,
        'order_send_name' => $order->order_send_name,
        'created_time' => date("Y-m-d H:i:s", $order->created_at),
        'pay_time' => $order->pay_at ? date("Y-m-d H:i:s", $order->pay_at) : '/',
        'send_time' => $order->send_at ? date("Y-m-d H:i:s", $order->send_at) : '/',
        'receive_time' => $order->receive_at ? date("Y-m-d H:i:s", $order->receive_at) : '/',
        'refund_start_time' => $order->refund_start_at ? date("Y-m-d H:i:s", $order->refund_start_at) : '/',
        'refund_success_time' => $order->refund_success_at ? date("Y-m-d H:i:s", $order->refund_success_at) : '/',
        'good_num_total' => count($good_data),
        'good_list' => $good_data,
      ];
      return Re::data(['data' => $order_data]);
    }

    //发货
    public function actionSendOrder() {
      $id = Yii::$app->request->post('id', 0);
      $send_name = Yii::$app->request->post('send_name', '');
      $send_code = Yii::$app->request->post('send_code', '');
      $order = OrderInfo::findOne($id);
      $order->state = 2;
      $order->order_send_name = $send_name;
      $order->order_send_code = $send_code;
      $order->save();
      return Re::data();
    }

    //订单退款列表
    public function actionOrderRefundList() {
      $order_sn = Yii::$app->request->post('order_sn', 0);
      $user_name = Yii::$app->request->post('user_name', 0);
      $user_phone = Yii::$app->request->post('user_phone', 0);
      $page = Yii::$app->request->post('page', 0);
      $page_size = Yii::$app->request->post('page_size', 10);
      $created_range = Yii::$app->request->post('created_range', '');
      $order_query = OrderInfo::find()
        ->leftJoin("user", 'order_info.user_id = user.id')
        ->where(['order_type' => 2, 'refund_state' => 0]);
      if ($order_sn) {
        $order_query->andWhere(['like', 'order_sn', $order_sn]);
      }
      if ($user_name) {
        $order_query->andWhere(['like', 'user.name', $user_name]);
      }
      if ($user_phone) {
        $order_query->andWhere(['like', 'user.phone', $user_phone]);
      }
      if ($created_range) {
        $data_arr = explode("-", $created_range);
        $start_at = $data_arr[0];
        $end_at = $data_arr[1]+86400;
        $order_query->andWhere("refund_start_at >= '$start_at' and refund_start_at <= '$end_at'");
      }
      $order_list = $order_query->orderBy('refund_start_at desc')->limit($page_size)->offset($page_size*$page)->all();
      $order_data = [];
      if ($order_list) {
        foreach ($order_list as $v) {
          $order_data[] = [
            'id' => $v->id,
            'order_sn' => $v->order->order_sn,
            'refund_money' => $v->order->money,
            'user_name' => $v->user->name,
            'user_phone' => $v->user->phone,
            'refund_img' => $v->refund_imgs,
            'refund_reason' => $v->refund_reason,
            'created_time' => date('Y-m-d H:i:s', $v->created_at),
          ];
        }
      }
      return Re::data([
        'data' => $order_data,
        'totalPage' => $order_query->count(),
        'currentPage' => $page,
        'perPage' => $page_size
      ]);
    }

    //审核退款
    public function actionAuditOrder() {
      $id = Yii::$app->request->post('id', 0);
      $result = Yii::$app->request->post('result', 1);
      $reason = Yii::$app->request->post('reason', '');
      $order = OrderInfo::findOne($id);
      $order->state = $result == 1 ? 5 : 7;
      $order->refund_state = $result;
      $order->audit_reason = $reason;
      $order->audit_user_id = Yii::$app->user->id;
      if ($result == 1) {
        /*$res = AliRefund::refund($order, $order->money);
        if ($res->code == 'success') {
          $order->state = 6;
        }*/
        $order->state = 6;
      }
      $order->save();
      return Re::data();
    }
}
